package com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.repository.impl;

import com.css.fxfzxczh.modules.evaluationSummary.vo.ParamsVo;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.entity.OtherLoss;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.repository.OtherLossRepository;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.vo.ResultVo;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/02/17
 * @Description: 其他类地震损失报表
 */
@Repository
public class OtherLossRepositoryImpl implements OtherLossRepository {

    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private static final String TABLE_NAME = "xczh_other_loss"; // 表名

    @Override
    public void deleteByXlrmId(String xlrmId) {
        String sql = "delete from  "+TABLE_NAME+"  where xlrm_id= '"+xlrmId+"'" ;
        jdbcTemplate.update(sql);
    }

    @Override
    public void batchSave(List<OtherLoss> list) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(list.toArray());
        String sql = "INSERT INTO "+TABLE_NAME +
                " ( id,xlrm_id,fill_form_user,tel,fill_form_time,province,city,county,township,village," +
                "item_type,unit_measurement,price,quantity,loss,comment,sequence)" +
                "values (:id,:xlrmId,:fillFormUser,:tel,:fillFormTime,:province,:city,:county,:township,:village," +
                ":itemType,:unitMeasurement,:price,:quantity,:loss,:comment,:sequence)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public List<ResultVo> getTotalLoss(ParamsVo paramsVo2) {
        String sql = "select t1.loss  " +
                "from "+TABLE_NAME+" t1 left join xczh_loss_report_manage t2 on t1.xlrm_id=t2.id " +
                "where t2.xsem_id = '"+paramsVo2.getXsemId()+"' and t2.report_type = '"+paramsVo2.getReportType()+"' and t1.sequence='合计'" +
                "and province='"+paramsVo2.getProvince()+"' and city ='"+paramsVo2.getCity()+"' and county='"+paramsVo2.getCounty()+"'";
        List<ResultVo> list = namedParameterJdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ResultVo.class));
        return list;
    }

    @Override
    public List<OtherLoss> getOtherLoss(String xsemId,String reportType) {
        String sql = "select t1.*  " +
                "from "+TABLE_NAME+" t1 left join xczh_loss_report_manage t2 on t1.xlrm_id=t2.id " +
                "where t2.xsem_id = '"+xsemId+"' and t2.report_type = '"+reportType+"' and t1.sequence='合计'";
        List<OtherLoss> list = namedParameterJdbcTemplate.query(sql, new BeanPropertyRowMapper<>(OtherLoss.class));
        return list;
    }
}
